﻿//客户
define('customer/index',function() {
	var ViewModel = function() {
		var self = this;
		// 这里是分页配置,第一个参数是查询的地址，第二个参数是分页大小，第三个参数是回调函数
		self.DataGrid = new RIT.eW.DataGridAjax('/customer/findAllCustomer',
				20, self.customer_process);
		self.customer_process = function(customers) {
			if (customers && customers.length && customers.length > 0) {
				$.each(customers, function(index, val) {
					val.checked = false;
				});
			}
			var items = ko.mapping.fromJS(customers);
			return items();
		};

		self.filter = ko.observable({
			name : '',
			code : '',
			contact : '',
			phone : '',
			email : ''
		});
		// 清空
		self.empty = function() {
			self.filter({
				name : "",
				code : "",
				contact : "",
				phone : "",
				email : ""
			})
		}
		// 查询
		self.search = function() {
			var data = self.filter();
			console.log(data);
			self.DataGrid.GridParams.pageIndex(1);
			self.DataGrid.GetData(data);
		};
		// 添加弹出框
		self.addCustomer = function() {
			layer.open({
				type : 1,
				title : "添加客户信息",
				area : [ '550px', '480px' ],
				content : $('#pop_div_customer')
			});
			self.model({
				name : '',
				code : '',
				ciqCode : '',
				crNumber : '',
				contact : '',
				phone : '',
				email : '',
				address : "",
				customerType : "",
				defaultLogistics : "",
				valid : "",
				note : ""
			})
		};
		// 修改
		self.updateCustomer = function(customer) {
			layer.open({
				type : 1,
				title : "修改客户信息",
				area : [ '550px', '480px' ],
				content : $('#pop_div_customer')
			});
			self.model(customer);
		};
		// 客户信息
		self.model = ko.observable({
			name : '',
			code : '',
			ciqCode : '',
			crNumber : '',
			contact : '',
			phone : '',
			email : '',
			address : "",
			customerType : "",
			defaultLogistics : "",
			valid : "",
			note : ""
		});
		// 关闭客户窗口信息
		self.layerClose = function() {
			layer.close(layer.index);
			toastr.info("已关闭");
		};

		// 保存客户信息
		self.submit = function() {
			var data = self.model();
			console.log(data);
			$.ajax({
				url : "customer/addOrUpdateCustomer",
				type : 'post',
				contentType : 'application/json',
				data : JSON.stringify(data),
				dataType : 'json',
				cache : false,
				success : function(result) {
					if (result.status == 0) {
						toastr.success(result.data, result.msg);
						layer.close(layer.index);
						self.search();
					} else {
						toastr.error(result.msg);
					}
				}
			})
		}

		// 改变客户的状态
		self.changeState = function(customer) {
			layer.confirm('确定改变客户的状态吗？', {
				btn : [ '确定', '取消' ]
			// 按钮
			}, function() {
				layer.msg('确定修改', {
					icon : 1
				});
				$.ajax({
					url : "customer/changeStateCustomer",
					type : 'post',
					contentType : 'application/json',
					data : JSON.stringify(customer),
					dataType : 'json',
					cache : false,
					success : function(result) {
						if (result.status == 0) {
							toastr.success(result.data, result.msg);
							self.search();
						} else {
							toastr.error(result.msg);
						}
					},
				});
			}, function() {
				layer.msg('已经取消', {
					time : 1000, // 20s后自动关闭
				});
			});
		}

		// 删除
		self.deleteCustomer = function(customer) {
			// 询问框
			layer.confirm('你确定要删除吗？删除后无法恢复！', {
				btn : [ '确定', '取消' ]
			// 按钮
			}, function() {
				$.ajax({
					url : "customer/deleteCustomer",
					type : 'post',
					contentType : 'application/json',
					data : JSON.stringify(customer),
					dataType : 'json',
					cache : false,
					success : function(result) {
						if (result.status == 0) {
							toastr.success(result.data, result.msg);
							self.search();
						} else {
							toastr.error(result.msg);
						}
					},
				});
				layer.msg('已删除', {
					icon : 1
				});
			}, function() {
				layer.msg('已经取消', {
					time : 1000, // 1s后自动关闭
				});
			});
		}
		self.search();
	};
	var model = new ViewModel();
	ko.applyBindings(model);
});